#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <cstdlib>

int main()
{
    mkfifo("input.fifo", 0666);
    mkfifo("output.fifo", 0666);

    size_t cmd_len;
    if (!fork())
    {
        system("/bin/sh 0<input.fifo 1>output.fifo 2>&1");
        printf("/bin/sh end\n");
        //execl("/bin/sh", "0<input.fifo 1>output.data 2>&1", NULL);
        //execl("/bin/sh", "1>output.fifo 2>&1", NULL);
    }
    else
    {
        if (!fork())
        {
            size_t buf_size = 1000;
            char *buf = new char[buf_size];
            int fd_output = open("output.fifo", O_RDONLY);
            printf("fd_output = %d\n", fd_output);
            while (1)
            {
                int read_len = read(fd_output, buf, buf_size);
                buf[read_len] = 0;
                printf("%s", buf);
            }
            close(fd_output);
            delete[] buf;
        }
        else
        {
            printf("here~\n");
            size_t cmd_size = 1000;
            char *cmd = new char[cmd_size];
            int fd_input = open("input.fifo", O_WRONLY);
            while (1)
            {
                cmd_len = getline(&cmd, &cmd_size, stdin);
                int write_len = write(fd_input, cmd, cmd_len);
            }
            close(fd_input);
            delete[] cmd;
        }
    }
}